package com.cpi.cornpp.futurepurchase.action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.futurepurchase.exception.FuturePurchaseException;
import com.cpi.cornpp.futurepurchase.services.FuturePurchaseService;



public class FuturePurchaseDeleteAction extends Action {
	/** Default forward*/
	private final static String DEFAULT = "default";//$NON-NLS-1$
	/** Identifier for the future purchase id*/
	private static final String FUTURE_PURCHASE_ID = "fpId";//$NON-NLS-1$
	/** Success delete key */
	private static final String SUCCESS_DELETE_LABEL = "common.delete.success";//$NON-NLS-1$
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	/** Error message name*/
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	/*
	 * (non-Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws FuturePurchaseException {
		FuturePurchaseService futurePurchaseService = null;
		int futurePurchaseId = 0;
		MessageResources messageResources = null;
		HttpSession httpSession = null;
		String messageError = null;
		//String relatedTo = null;		
		try{
			httpSession = request.getSession(false);
			messageResources = getResources(request);
			futurePurchaseId = Integer.parseInt(request.getParameter(FUTURE_PURCHASE_ID));
			futurePurchaseService = new FuturePurchaseService();
			futurePurchaseService.deleteFuturePurchase(futurePurchaseId);
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_DELETE_LABEL);
			httpSession.setAttribute(MESSAGE, messageError);				
		}catch( FuturePurchaseException futurePurchaseException ){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), futurePurchaseException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, futurePurchaseException);
				//form.reset(mapping, request);
				throw futurePurchaseException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, messageError);
			}
		}catch( Exception exception ){
			FuturePurchaseException futurePurchaseException = null;
			exception.printStackTrace(System.out);
			futurePurchaseException= new FuturePurchaseException(exception , CornPPException.LAYER_ACTION,CornPPException.ACTION_DELETE);
			httpSession.setAttribute(CUSTOM_EXCEPTION, futurePurchaseException);
			throw futurePurchaseException;
		}
		return mapping.findForward(DEFAULT);
	}
}